#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''translate with youdao dictory
'''

__revision__ = '0.1'
__author__ = 'lxd'

from xml.dom import minidom
import urllib2

def translate(word):
    f = getTranslate(word)
    data = f.read()
    return parse(data)

def getTranslate(word):
    url = 'http://dict.youdao.com/fsearch?client=deskdict&keyfrom=chrome.extension&q=%s&pos=-1&doctype=xml&xmlVersion=3.2&dogVersion=1.0&vendor=unknown&appVer=3.1.17.4208&le=eng' %word
    f = urllib2.urlopen(url, timeout = 5)
    return f

def parse(xml_data):
    xmldom = minidom.parseString(xml_data)
    root = xmldom.documentElement
    translates = root.getElementsByTagName('translation')
    r = ''
    for t in translates:
        content = t.getElementsByTagName('content')[0]
        node = content.childNodes[0]
        r += node.data+'; '
    return r

if __name__ == '__main__':
    print translate('family')
    #print parse()

